import React from 'react';
import { Spin } from 'antd';
import { isEqual, isEmpty } from 'lodash';
import {WeaErrorPage, WeaTools, WeaTable, WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
const { ls } = WeaTools;
import Title from './Title';
import MarqueeCom from '../common/MarqueeCom';
import { formatData,setContentviewHeight } from '../../util/common';
import WfCenterLogList from './WfCenterLogList';
//内容组件
class EContent extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            tabdata: props.edata.data,
            currentTab: props.edata.currenttab,
            refresh: false,
        }

        this.handleTabData = this.handleTabData.bind(this);
        this.openWfCenterLogList = this.openWfCenterLogList.bind(this);
        if(window.element_workflow_refresh && typeof window == 'object'){
            window.element_workflow_refresh[props.config.params.eid] = this.handleTabData.bind(this);
        }else{
            let obj = {};
            obj[props.config.params.eid] = this.handleTabData.bind(this);
            window.element_workflow_refresh = obj
        }
    }
    componentDidMount(){
        this.props.onRef(this);
        this.openWfCenterLogList();
        try{
            if(window._ListenerCtrl) _ListenerCtrl.bind();
        } catch(e){
            window.console ? console.log(getLabel(388033,"流程元素Ctrl绑定出错了："),e) : alert(getLabel(388033,"流程元素Ctrl绑定出错了：")+e);
        }
    }
    componentWillUnmount(){
        try{
            if(window._ListenerCtrl) _ListenerCtrl.unbind();
        } catch(e){
            window.console ? console.log(getLabel(388034,"流程元素Ctrl解除绑定出错了："),e) : alert(getLabel(388034,"流程元素Ctrl解除绑定出错了：")+e);
        }
    }
    componentWillReceiveProps(nextProps, nextState){
        if(!isEqual(this.props, nextProps)){
            this.setState({
                tabdata: nextProps.edata.data,
                currentTab: nextProps.edata.currenttab,
                refresh: false,
            });
        }
    }
    shouldComponentUpdate(nextProps, nextState){
        return !isEqual(this.state, nextState) || !isEqual(this.props, nextProps)
    }
    handleTabData(tabid){
        let tabid_fr = tabid;
        const { config } = this.props;
        let { tabdata={} } = this.state;
        const { tabsetting={} } = tabdata;
        const { more } = tabsetting;
        const { params, isremembertab } = config;
        const { currentTab } = this.state;
        if(!tabid) tabid = currentTab;
        tabdata = ls.getJSONObj('tabdata-' + params.eid + '-' + tabid+ '-' +config.userid) == null ? {} : ls.getJSONObj('tabdata-' + params.eid + '-' + tabid+ '-' +config.userid);
        // if(!tabid_fr){ // 点击流程列表标题时
        //     if(tabdata){
        //         if(more) {
        //             let arr = more.split(',');
        //             let pass = false;
        //             //判断是否是待办
        //             for(let i = 0; i<arr.length; i++){
        //                 if(arr[i] == "\"viewType\":\"1\""){
        //                     pass = true;
        //                     break;
        //                 }
        //             }
        //             if(!pass) return;
        //         }
        //     }
        // }
        this.setState({
            refresh: tabid !== currentTab,
            currentTab: tabid,
            tabdata,
        });
        if(isremembertab){
            let edata = ls.getJSONObj('edata-' + params.eid+ '-' +config.userid);
            if(edata){
                edata.currenttab = tabid;
                edata.data = tabdata;
                ls.set('edata-' + params.eid+ '-' +config.userid, edata);
            }
        }
        WeaTools.callApi('/api/portal/element/workflowtab', 'POST', {...params, tabid}).then(result => {
            ls.set('tabdata-' + params.eid + '-' + tabid+ '-' +config.userid, result);
            if(tabid === this.state.currentTab){

                this.setState({
                    tabdata: result,
                    refresh: false,
                });
                if(isremembertab){
                    let edata = ls.getJSONObj('edata-' + params.eid+ '-' +config.userid);
                    if(edata){
                        edata.currenttab = tabid;
                        edata.data = result;
                        ls.set('edata-' + params.eid+ '-' +config.userid, edata);
                    }
                }
            }
        });
    }
    openWfCenterLogList(params,params1,params2) {
        this.refs.wfCenterLogList && this.refs.wfCenterLogList.signviewPoistion(params,params1,params2);
    }
    render() {
        const { config, edata, handleRefresh, handleDelete, fontSizeMp } = this.props;
        const { tabdata={}, currentTab, refresh } = this.state;
        const { tabsetting={}, dataColoums=[] } = tabdata;
        const { tabids, titles, counts, viewtypes, esetting } = edata;
        const { eid, ebaseid } = config.params;
        let { fontStyle,header } = config.item;
        if(!isEmpty(fontSizeMp)) fontStyle = fontSizeMp;
        esetting.fontStyle = fontStyle;
        esetting.dataColoums = dataColoums;
        const { contentview = {} } = config.item;
        let cstyle = {height: contentview.style.height,overflow: "auto"};
        if(header.displayPosition && header.displayPosition == 'bottom'){
            if(tabids && tabids.length > 1 || tabids && tabids.length == 1 && esetting.isAlwaysShow=='1'){
                if(contentview.style.height){
                    if(esetting.tabstyle == '1' && esetting.tabvalue){
                        cstyle = setContentviewHeight(contentview.style.height,esetting.tabvalue)
                    }else{
                        cstyle = {height: parseInt(contentview.style.height)-32+"px",overflow: "auto"};
                    }   
                }else {
                    cstyle = {height: contentview.style.height,overflow: "auto"};
                }
                let elementH=parseInt(contentview.style.height)-parseInt($(`#titleContainer_${eid}`).css('height'));
                if(elementH!=parseInt(cstyle.height)){
                    if(elementH<parseInt(cstyle.height)){
                        cstyle.height=elementH+"px";
                    }
                }
            }
        }
        let contentHtml = <div/>;
        let more = "{}";
        let currCount = "";
        if (tabdata && tabdata.data) {
            for (var i = 0; i <tabids.length; i++) {
                currCount = tabsetting.count;
                if(currCount){
                    let cc=parseInt(currCount);
                    if (tabids[i] === currentTab) {
                        counts[i] = currCount;
                    }
                }
            }
            more = tabsetting.more;
            const list = tabdata.data.map((item, index) => {
                return {key: index, ...item};
            });
            contentHtml = <MarqueeCom ecId={`${this && this.props && this.props.ecId || ''}_MarqueeCom@s4ysk9`} tabdata={tabdata} eid={eid} scolltype={tabdata.tabsetting.scrollDirection} scrollSpeed={tabsetting.scrollSpeed} ebaseid={ebaseid}><div className="portal_element_workflow"><WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@x9vk4c`} columns={formatData(list[0], esetting,this.openWfCenterLogList )} showHeader={esetting.showHeader == '1'} pagination={false} dataSource={list} size="small"/></div></MarqueeCom>
        }
        const _titleProps = {
            currentTab,
            config,
            titles,
            tabids,
            counts,
            more,
            currCount,
            handleRefresh,
            handleDelete,
            fontSizeMp,
            eMoreAddress: tabsetting.eMoreAddress,
            eMoreShowType: tabsetting.eMoreShowType,
            tabwidth: esetting.tabwidth,
            methodvalue: esetting.methodvalue,
            isAlwaysShow: esetting.isAlwaysShow,
            tabstyle: esetting.tabstyle,
            tabvalue: esetting.tabvalue,
            handleTabData: this.handleTabData
        }
        return <div>
            <Title ecId={`${this && this.props && this.props.ecId || ''}_Title@h3l4ok`} {..._titleProps}/>
            <div className="tabContant" id={`tabcontant_${eid}`} style={{height: cstyle.height}}>
                <div className="workflowtab">
                <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@fa8pbr`} spinning={refresh}>{contentHtml}</Spin>
                </div>
                <WfCenterLogList ecId={`${this && this.props && this.props.ecId || ''}_WfCenterLogList@je8ahl`} eid={eid} tabid={currentTab} refresh={refresh} more={more} ref="wfCenterLogList"/>
            </div>
        </div>;
    }
}

class MyErrorHandler extends React.Component {
    render() {
        const hasErrorMsg = this.props.error && this.props.error !== "";
        return ( <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@ke6l9d`} msg = { hasErrorMsg ? this.props.error : getLabel(385218,"对不起，该页面异常，请联系管理员！")  }/>
        );
    }
}
EContent = WeaTools.tryCatch(React, MyErrorHandler, { error: "" })(EContent);
export default EContent;



// WEBPACK FOOTER //
// ./pc/portal_fs/elements/components/workflow/EContent.js